package light.head.dao.inv;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.inv.InvBusinessType;
@Service
public class InvBusinessTypeDAO extends BaseDAO<InvBusinessType, Serializable>{
	private final static String sql_findAll = "FROM InvBusinessType iu WHERE iu.oc = ?";
	
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if (ocs == null ) return null;
		
		StringBuffer hql = new StringBuffer(" FROM InvBusinessType ibt WHERE ibt.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if (null != map) {
			String name = map.get(Parm.NAME);
			String cu = map.get(Parm.CU);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			
			
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND ibt.deleted = ?");
			}
			if (isNotNull(name)) {
				list.add("%" + name + "%");
				hql.append(" AND ibt.name LIKE ?");
			}if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND ibt.ct >= ?");
			}
			if (isNotNull(cu)) {
				list.add(Integer.parseInt(cu));
				hql.append(" AND ibt.cu = ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append(" AND ibt.ct >= ?");
			}
		}
		hql.append(" ORDER BY ibt.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	public List<InvBusinessType> findByNameOc(String name, Integer oc){
		if (name == null || oc == null) return null;
		StringBuffer hql = new StringBuffer("FROM InvBusinessType ibt WHERE ibt.name = ? AND ibt.oc = ? ");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(name);
		parm.add(oc);
		
		return find(hql.toString(), parm.toArray());
	}
	public List<InvBusinessType> findAll(Integer oc) {
		List<InvBusinessType> list = find(sql_findAll, oc);
		return list;
	}
}
